package info.xiao.dataStructrue.array;

/**
 * @Classname FirstMissingPositive
 * @Description 缺失的第一个正数
 * @Url https://leetcode.cn/problems/first-missing-positive/
 * @Date 2023/4/6 10:02
 * @Created by 晓龙Oba
 */
public class FirstMissingPositive {
    public static void main(String[] args) {
        int nums[] = {0, 1, 2};
        int result = firstMissingPositive(nums);
        System.out.println(result);
    }

    private static int firstMissingPositive(int[] nums) {
        int result = nums.length + 1;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] <= 0 || nums[i] > nums.length) {
                nums[i] = nums.length + 1;
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if (Math.abs(nums[i]) != nums.length + 1) {
                nums[Math.abs(nums[i]) - 1] = 0 - Math.abs(nums[Math.abs(nums[i]) - 1]);
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] >= 0) {
                return i + 1;
            }
        }

        return result;
    }
}
